/*
 *  puImageButton.cpp
 *  trail-map-editor
 *
 *  Created by Giorgio Tino on 2/18/13.
 *  Copyright 2013 __MyCompanyName__. All rights reserved.
 *
 */

#include "ui.h"

uiButton::uiButton ( int minx, int miny, int maxx, int maxy) : puButton ( minx, miny, maxx, maxy )
, normalImage(0)
, focusedImage(0)
, clickedImage(0)
, disabledImage(0)
{
}

uiButton::uiButton ( int minx, int miny, int maxx, int maxy, const char *name, const char *nameFocused, const char *nameClicked, const char *nameDisabled) :
puButton ( minx, miny, maxx, maxy )
{
    if(name) normalImage = new uiImage(name, maxx - minx, maxy - miny);
    if(nameFocused) focusedImage = new uiImage(nameFocused, maxx - minx, maxy - miny);
    if(nameClicked) clickedImage = new uiImage(nameClicked, maxx - minx, maxy - miny);
    if(nameDisabled) disabledImage = new uiImage(nameDisabled, maxx - minx, maxy - miny);
}

void uiButton::setNormalImage(const char* name, const float width, const float height)
{
    if(normalImage)
    {
        delete normalImage;
        normalImage = 0;
    }
    
    normalImage = new uiImage(name, width, height);
}

void uiButton::draw ( int dx, int dy )
{
	if ( !visible || ( window != puGetWindow () ) ) return ;
	
	float x = abox.min[0] + dx;
	float y = abox.min[1] + dy;
	
	if(!isActive() && disabledImage)
		disabledImage->draw(x, y);
	else if (isHighlighted() && clickedImage)
        clickedImage->draw(x,y);
    else if(!isHighlighted() && normalImage)
        normalImage->draw(x, y);
    else
        abox.draw ( dx, dy, style, colour, FALSE, border_thickness ) ;
	
	if ( r_cb )
		r_cb ( this, dx, dy, render_data ) ;
	else
		draw_legend ( dx, dy ) ;
	
	draw_label ( dx, dy ) ;
}